#define _GNU_SOURCE
#include <netinet/if_ether.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <linux/if_tun.h>
#include <net/if.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <errno.h>
#include <unistd.h>
int main()
{
    char tap_name[] = "tap0";
    int fd = open("/dev/net/tun", O_RDWR);

    struct ifreq ifr;
    memset(&ifr, 0, sizeof(struct ifreq));
    strncpy(ifr.ifr_ifrn.ifrn_name, tap_name, sizeof(tap_name));
    ifr.ifr_ifru.ifru_flags = IFF_TAP | IFF_NO_PI;
    if (ioctl(fd, TUNSETIFF, &ifr) != 0)
    {
        perror(strerror(errno));
        exit(errno);
    };

    while (1)
    {
        /* code */
        char buf[1024] = {0};
        int read_count = 0;
        if ((read_count = read(fd, buf, 1024)) == -1)
        {
            perror(strerror(errno));
            exit(errno);
        };
        struct ether_header header;
        memcpy(&header, buf, sizeof(struct ether_header));
        if (header.ether_type != 50)
            continue;
        printf("ether_dhost : ");
        for (int j = 0; j < ETH_ALEN; j++)
        {
            /* code */
            printf("%02x ", header.ether_dhost[j]);
        }
        printf("\n");
        printf("ether_shost : ");
        for (int j = 0; j < ETH_ALEN; j++)
        {
            /* code */
            printf("%02x ", header.ether_shost[j]);
        }
        printf("\n");
        printf("ether_type %d\n", header.ether_type);
        printf("struct ether_header size %zd\n", sizeof(struct ether_header));
        printf("read count %d\n", read_count);
    }
    return 0;
}